//
//  ImagePicker.m
//  TestAfn2
//
//  Created by xujingjing on 16/7/12.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "ImagePicker.h"

#import "UzysAssetsPickerController.h"


@interface ImagePicker ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UzysAssetsPickerControllerDelegate>{
    
    UIImagePickerController *picker;
    
    NSMutableArray *imagesArr;
}


@property (copy, nonatomic) pickerImagePic pickerImagePic;
@property (assign, nonatomic) BOOL allowsEditing;
@property (weak, nonatomic) id vc;  //设置代理
@property (assign, nonatomic) NSInteger integer;



@property (copy, nonatomic) imagesArrPic imagesArrPic;
@property (copy, nonatomic) imagesURLArr imagesURLArr;
@property (copy, nonatomic) imageNamesArr imageNamesArr;
@property(nonatomic,assign) int i;
@property(nonatomic,assign) long j;

@end

@implementation ImagePicker

static ImagePicker *sharedManager = nil;

+ (ImagePicker *)sharedManager {
    
    @synchronized (self) {
        
        if (!sharedManager) {
            
            sharedManager = [[[self class] alloc] init];
           
        }
        
    }
    
    return sharedManager;
}

- (void)setPresentDelegateVC:(id)vc
          InfoDictionaryKeys:(NSInteger)integer
          isAllowMutilSelece:(BOOL)isAllowMutilSelece
           choosePhotoMaxNum:(NSInteger)choosePhotoMaxNum{
   
    picker = [[UIImagePickerController alloc]init];
    
    picker.delegate = self;
    
    self.integer = integer;
    
    picker.allowsEditing = YES;
    
    self.allowsEditing = picker.allowsEditing;
    
    self.vc = vc;

    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"上传照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        if (isAllowMutilSelece) {
            
            [self chooseImageFromAlbum:choosePhotoMaxNum];

            return ;
        }
        [self presentPickerVC:picker];
    }];
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备不支持拍照" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            return;
            
        }
        [self presentPickerVC:picker];
    }];

    [alertC addAction:cancelAction];
    [alertC addAction:albumAction];
    [alertC addAction:takePhotoAction];
    [self.vc presentViewController:alertC animated:YES completion:nil];
    

}
- (void)presentPickerVC:(UIViewController *)pickerVC{

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunicode-whitespace"
    
    [self.vc presentViewController:pickerVC animated:YES completion:nil];
    
#pragma clang diagnostic pop

}
- (void)dwGetpickerImage:(pickerImagePic)pickerImagePic{
    
    self.pickerImagePic = ^(UIImage *image) {
        
        pickerImagePic(image);
        
    };
}

- (void)getMutilImage:(imagesArrPic)imagesArrPic imagesURLArr:(imagesURLArr)imagesURLArr imageNamesArr:(imageNamesArr)imageNamesArr{
    
    self.imagesArrPic = ^(NSMutableArray *imageArr){
        
        imagesArrPic(imageArr);
    };

    self.imagesURLArr = ^(NSMutableArray *uRLArr){
        
        imagesURLArr(uRLArr);
    };
    self.imageNamesArr = ^(NSMutableArray *namesArr){
        
        imageNamesArr(namesArr);
    };

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


    UIImage *image = [[UIImage alloc] init];
    
    NSArray *array = @[@"UIImagePickerControllerMediaType",
                       @"UIImagePickerControllerOriginalImage",
                       @"UIImagePickerControllerEditedImage",
                       @"UIImagePickerControllerCropRect",
                       @"UIImagePickerControllerMediaURL",
                       @"UIImagePickerControllerReferenceURL",
                       @"UIImagePickerControllerMediaMetadata",
                       @"UIImagePickerControllerLivePhoto"];
    
    if (self.integer) {
        
        image = [info objectForKey:array[self.integer]];
        
    }else {
        
        image = [info objectForKey:array[2]];
        
    }
   
    
    if (self.pickerImagePic) {
        
        self.pickerImagePic(image);
        
    }
    
    [self.vc dismissViewControllerAnimated:YES completion:nil];



}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [self.vc dismissViewControllerAnimated:YES completion:nil];
    
}


-(void)chooseImageFromAlbum:(NSInteger)choosePhotoMaxNum
{
    
    UzysAssetsPickerController *pickerUzys = [[UzysAssetsPickerController alloc] init];
    pickerUzys.didSelectedPhotoNumber = imagesArr.count;
    pickerUzys.maximumNumberOfSelectionPhoto = choosePhotoMaxNum;
    pickerUzys.delegate = self;
    [self presentPickerVC:pickerUzys];

}

#pragma mark多张图片
- (void)UzysAssetsPickerController:(UzysAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
  
    imagesArr  = [NSMutableArray array];

    NSMutableArray *imagesURL = [NSMutableArray array];
    NSMutableArray *imageNames = [NSMutableArray array];

    NSString * assetPropertyType = (NSString *)[assets[0] valueForProperty:@"ALAssetPropertyType"];
    if([assetPropertyType isEqualToString:@"ALAssetTypePhoto"]) //Photo
    {
        [assets enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            ALAsset *representation = obj;
            UIImage *img = [UIImage imageWithCGImage:representation.defaultRepresentation.fullScreenImage
                            ];
            
            [imagesArr addObject:img];
            NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            self.i++;
            NSString *fileName = [NSString stringWithFormat:@"mainTanceee%d.jpg",self.i];
            NSString *filePath = [[paths objectAtIndex:0] stringByAppendingString:fileName];
            [UIImageJPEGRepresentation([ALTools imageWithImage:img], 0.9) writeToFile:filePath atomically:YES];
            [imagesURL addObject:filePath];
            [imageNames addObject:fileName];
            
            if (self.imagesArrPic) {
                
                self.imagesArrPic(imagesArr);
                
            }
            if (self.imagesURLArr) {
                
                self.imagesURLArr(imagesURL);
                
            }
            if (self.imageNamesArr) {
                
                self.imageNamesArr(imageNames);
                
            }
            
            
        }];
    }

}



@end
